package com.cloudlinkin.util.redis;


import static com.cloudlinkin.util.redis._JedisPoolQueue.getJedis;
import static com.cloudlinkin.util.redis._JedisPoolQueue.returnResource;

import com.cloudlinkin.util.error.RecordingExceptionControl;
import org.apache.log4j.Logger;



import redis.clients.jedis.Jedis;

/*
 * redis 队列kit
 */
public class _RedisQueue {
	Logger log = Logger.getLogger(this.getClass());

	public boolean lpush(String key, String value) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			if (jedis != null) {
				jedis.lpush(key, value);
				RecordingExceptionControl.controlClear();
				return true;
			} else {
				RecordingExceptionControl.controlRecordingException("RedisQueue",
						"Mail-_RedisQueue-lpush:jedis is null");
			}
			return false;
		} catch (Exception e) {
			RecordingExceptionControl.controlRecordingException("RedisQueue", "Mail-_RedisQueue-lpush:"+e.getMessage());
			log.error("", e);
			return false;
		} finally {
			returnResource(jedis);
		}

	}

	public boolean rpush(String key, String value) {
		Jedis jedis = null;
		try {
			jedis = getJedis();

			if (jedis != null) {
				jedis.rpush(key, value);
				RecordingExceptionControl.controlClear();
				return true;
			} else {
				RecordingExceptionControl.controlRecordingException("RedisQueue",
						"Mail-_RedisQueue-rpush:jedis is null");
			}
			return false;
		} catch (Exception e) {
			log.error("", e);
			RecordingExceptionControl.controlRecordingException("RedisQueue", "Mail-_RedisQueue-rpush:"+e.getMessage());
			return false;
		} finally {
			returnResource(jedis);
		}
	}

	public String blpop(String... keys) {
		Jedis jedis = null;
		jedis = getJedis();
		try {
			if (jedis != null) {
				return jedis.blpop(0, keys).get(1);
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			returnResource(jedis);
		}
	}

	public String brpop(String... keys) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			if (jedis != null) {
				return jedis.brpop(0, keys).get(1);
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 清空redis 所有数据
	 * 
	 * @return
	 */
	public boolean flushDB() {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			if (jedis != null) {
				jedis.flushDB();
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 检查是否连接成功
	 * 
	 * @return
	 */
	public String ping() {
		Jedis jedis = getJedis();
		try {
			if (null != jedis) {
				return jedis.ping();
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			returnResource(jedis);
		}
	}

	/**
	 * 查看redis里有多少数据
	 */
	public long dbSize() {
		try (Jedis jedis = getJedis();) {
			return jedis.dbSize();
		} catch (Exception e) {
			return 0l;
		}
	}

	/**
	 * 查看队列长度
	 */
	public long llen(String key) {
		try (Jedis jedis = getJedis();) {
			return jedis.llen(key);
		} catch (Exception e) {
			return 0l;
		}
	}
}
